import Vue from "vue";
import Router from "vue-router"
import store from "../store"


// 路由懒加载
const Login = () => import(/* webpackChunkName: "Login_Home_Welcome_ResetPwd" */ '../view/Login.vue')
const Home = () => import(/* webpackChunkName: "Login_Home_Welcome_ResetPwd" */ '../view/Home.vue')
const Welcome = () => import(/* webpackChunkName: "Login_Home_Welcome_ResetPwd" */ '../view/personal/Welcome.vue')

const Icons = () => import(/* webpackChunkName: "Icons" */ '../view/icons/index.vue')

const Users = () => import(/* webpackChunkName: "Users" */ '../view/users/User.vue')

const Authority = () => import(/* webpackChunkName: "Auth" */ '../view/authority/index.vue')
const Roles = () => import(/* webpackChunkName: "Auth" */ '../view/authority/Roles.vue')
const Menus = () => import(/* webpackChunkName: "Auth" */ '../view/authority/Menus.vue')
const API = () => import(/* webpackChunkName: "Auth" */ '../view/authority/Api.vue')

const System = () => import(/* webpackChunkName: "system" */ '../view/system/index.vue')
const Quill = () => import(/* webpackChunkName: "system" */ '../view/system/editor/quill.vue')
const Md = () => import(/* webpackChunkName: "system" */ '../view/system/editor/md.vue')
const ExcelUpload = () => import(/* webpackChunkName: "system" */ '../view/system/excel/upload-excel.vue')
const Form = () => import(/* webpackChunkName: "system" */ '../view/system/form/form.vue')
const FileManager = () => import(/* webpackChunkName: "system" */ '../view/system/file/fileManager.vue')

const Messages = () => import(/* webpackChunkName: "Message" */ '../view/message/Messages.vue')
const Notices = () => import(/* webpackChunkName: "Message" */ '../view/message/Notices.vue')
const Tasks = () => import(/* webpackChunkName: "Message" */ '../view/message/Tasks.vue')

const LoginLog = () => import(/* webpackChunkName: "Log" */ '../view/log/LoginLog.vue')
const OperLog = () => import(/* webpackChunkName: "Log" */ '../view/log/OperLog.vue')

const Error = () => import(/* webpackChunkName: "Error" */ '../view/404.vue')

// 注册路由
Vue.use(Router);

// 配置路由
const router = new Router({
    // mode: "history",
    routes: [
        { path: "/", redirect: "/home"},
        { path: "/Login", component: Login},
        {
            path: "/home",
            component: Home,
            redirect: "/welcome",
            children: [
                {path: "/welcome", component: Welcome},
                {path: "/users", component: Users, meta: {requireAuth: true, title: "用户列表"}},
                {path: "/icons", component: Icons, meta: {requireAuth: true, title: "图标集合"}},
                {
                    path: 'authority',
                    component: Authority,
                    children: [
                        {path: "roles", component: Roles, meta: {requireAuth: true, title: "角色列表"}},
                        {path: "menus", component: Menus, meta: {requireAuth: true, title: "菜单列表"}},
                        {path: "api", component: API, meta: {requireAuth: true, title: "API列表"}},
                    ]
                },
                {
                    path: "system",
                    component: System,
                    children: [
                        {path: "quill", component: Quill, meta: {requireAuth: true, title: "富文本编辑器"}},
                        {path: "markdown", component: Md, meta: {requireAuth: true, title: "markdown编辑器"}},
                        {path: "excel-upload", component: ExcelUpload, meta: {requireAuth: true, title: "Excel上传"}},
                        {path: "form", component: Form, meta: {requireAuth: true, title: "Form表单"}},
                        {path: "fileManager", component: FileManager, meta: {requireAuth: true, title: "文件管理"}},
                    ]
                },
                {path: "messages", component: Messages, meta: {requireAuth: true, title: "消息列表"}},
                {path: "notices", component: Notices, meta: {requireAuth: true, title: "通知列表"}},
                {path: "tasks", component: Tasks, meta: {requireAuth: true, title: "任务列表"}},
                {path: "loginLog", component: LoginLog, meta: {requireAuth: true, title: "登录日志"}},
                {path: "operLog", component: OperLog, meta: {requireAuth: true, title: "操作日志"}},
            ]
        },

        {path: "/404", component: Error},
        { path: "*", redirect: "/404"},
    ]
});

router.beforeEach((to, from, next) => {
    if (to.path === "/Login") return next()
    if (to.meta.requireAuth){
        if (!store.getters.menu_path_map.hasOwnProperty(to.path) ) {
            if (!store.getters.menu_path_map.hasOwnProperty("/" + to.name)) {
                return next('/404')
            }
        }
    }
    const tokenStr = window.sessionStorage.getItem("token");
    if (!tokenStr) return next("/Login")
    next()
})

export default router